/* Psychiatry data */
clear all
cd "I:\workdata\706727\Build\Data"
use PsykTestLongRunRD, clear

keep if inrange(treatagec,28,46)
keep if inrange(distyrm,-5,-0.5)

keep pnr dupli distyrm Dsui Dpsykhosp Dtherapy psychiatry Dpois_painkiller Dadep Danxsleep Dcash_any Ddisab_any Dcash_unemp2 Dsueduc Dsick Doutpat Dinpat Dmedphyc Dspecdoc Ddocvisit Der Dpsyc wealth inc earn docvisit specdoc famany_fam*  kom psykmed ssri adep psykmed ssri adep married koen

destring(kom),replace
merge m:1 kom using "H:\CAM_info\Utilities\OldNewMunicipalities\kom_komny.dta", keepusing(kom komny)
drop if _merge==2
drop _merge
drop kom
rename komny kom
destring(kom),replace

rename kom start
tostring(start),replace
merge m:1 start using "\\srvfsenas1\data\Formater\SAS formater i Danmarks Statistik\STATA_datasaet\Geokoder\c_komreg_v4_k"
rename start kom
drop if _merge==2
drop _merge

gen man=(koen==1)
drop koen 

tab kom, gen(muni)
tab KOMREG_V4_K, gen(reg)
drop kom KOMREG_V4_K

replace distyrm=floor(distyrm)

gcollapse (max) Dsui Dpsykhosp Dtherapy psychiatry Dpois_painkiller Dadep Danxsleep Dcash_any Ddisab_any Dcash_unemp2 Dsueduc Dsick Doutpat Dinpat Dmedphyc Dspecdoc Ddocvisit Der Dpsyc wealth inc earn docvisit specdoc famany_fam*  man married psykmed ssri adep  muni* reg*, by(pnr dupli distyrm) fast

replace distyrm=-distyrm

foreach var of varlist muni* reg* {
rename `var' `var'_
}

foreach var of varlist Dsui Dpsykhosp Dtherapy psychiatry Dpois_painkiller Dadep Danxsleep Dcash_any Ddisab_any Dcash_unemp2 Dsueduc Dsick Doutpat Dinpat Dmedphyc Dspecdoc Ddocvisit Der Dpsyc wealth inc earn docvisit specdoc famany_fam* man married psykmed ssri adep reg* muni* {

rename `var' cont_`var'

}

reshape wide cont*, i(pnr dupli) j(distyrm)

cd "I:\workdata\706727\Build\Data"
tempfile covars
save `covars'

**** Create final data ****
clear all
cd "I:\workdata\706727\Build\Data"
use PsykTestLongRunRD, clear

keep if inrange(treatagec,18,50)
keep if inrange(distyrm,-5,7)

g byte postreform=(tyrm>=2008.25) & !mi(tyrm)

foreach var in psyc docvisit specdoc medphyc inpat outpat er {
replace `var'=0 if `var'<0 | mi(`var')
}

/* Winzorize */
foreach var in psyc docvisit specdoc medphyc inpat outpat er {
qui sum `var' if `var'>0 & !mi(`var'), d
replace `var'=r(p99) if `var'>r(p99) & !mi(`var')
}

keep if inrange(treatagec,28,46)

replace yrm=floor(yrm*4)/4
replace tyrm=floor(tyrm*4)/4
cap drop distyrm*
gen distyrm=yrm-tyrm

gcollapse (mean) firstpsyc psyc docvisit specdoc medphyc inpat outpat er  , by(pnr dupli distyrm tyrm yrm treatagec postreform treat) fast

merge m:1 pnr dupli using `covars'
drop if _merge==2
drop _merge

order pnr dupli distyrm firstpsyc psyc docvisit specdoc medphyc inpat outpat er  *

gen age=treatagec+distyrm

preserve
drop postreform 

save "MLOutPred_Full", replace

restore

preserve
keep if postreform==0 
drop postreform

save "MLOutPred_Train", replace

restore

preserve
keep if postreform==1 
drop postreform 

save "MLOutPred_Test", replace

restore
